/***************************************************************************************
 *
 *  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
 *
 *  By downloading, copying, installing or using the software you agree to this license.
 *  If you do not agree to this license, do not download, install, 
 *  copy or use the software.
 *
 *  Copyright (C) 2014-2019, Happytimesoft Corporation, all rights reserved.
 *
 *  Redistribution and use in binary forms, with or without modification, are permitted.
 *
 *  Unless required by applicable law or agreed to in writing, software distributed 
 *  under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
 *  CONDITIONS OF ANY KIND, either express or implied. See the License for the specific
 *  language governing permissions and limitations under the License.
 *
****************************************************************************************/

#ifndef INDEX_HASH_H
#define INDEX_HASH_H

#include "../defineconfig.h"


#define IHASH_KEY_MAX_LEN		127

typedef struct index_hash_node
{
	uint32      bNodeValidate	:	1;		    // 该节点有效
	uint32	    bLinkValidate	:	1;		    // 该节点有冲突后续链表
	uint32	    bFreeList		:	1;		    // 该节点在FreeList中
	uint32	    reserved		:	29;
	char        key_str[IHASH_KEY_MAX_LEN+1];	// 索引字段实际值
	uint64	    index;				            // 用户单元索引/指针
	uint32	    next_index;					    // 冲突链数组索引
} IHASHNODE;

typedef struct index_hash_ctx
{
	uint32	    hash_num;					    // hash单元总数
	uint32	    link_num;					    // 冲突单元总数
	IHASHNODE *	hash_array;					    // hash单元
	IHASHNODE *	link_array;					    // 冲突单元
	void *		hash_semMutex;
	void *		link_semMutex;
	uint32	    link_index;
	IHASHNODE *	p_node;						    // 遍历查找使用的内部节点记录
	uint32	    conflict_cnt;				    // 冲突计数
} IHASHCTX;

#ifdef __cplusplus
extern "C" {
#endif

IHASHCTX *  ihash_init(uint32 hash_num, uint32 link_num);
void        ihash_uninit(IHASHCTX * p_ctx);
uint32      ihash_link_pop(IHASHCTX * p_ctx);
void        ihash_link_push(IHASHCTX * p_ctx, uint32 push_index);
uint32      ihash_index(IHASHCTX * p_ctx, const char * key_str);
BOOL        ihash_add(IHASHCTX * p_ctx, const char * key_str, uint64 index, int type);
BOOL        ihash_del(IHASHCTX * p_ctx, const char * key_str, uint64 index);
uint64      ihash_find_index_from_keystr(IHASHCTX * p_ctx, const char * key_str);
void        ihash_lock(IHASHCTX * p_ctx);
void        ihash_unlock(IHASHCTX * p_ctx);
void *      ihash_save_var(IHASHCTX * p_ctx);
void        ihash_restore_var(IHASHCTX * p_ctx, void * p_node);

#ifdef __cplusplus
}
#endif

#endif	// INDEX_HASH_H


